package me.zingon.web;

import me.zingon.model.Log;
import me.zingon.service.LogService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.mail.Session;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class IpFilter implements Filter {

	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest req=(HttpServletRequest) request; 
        HttpSession session=req.getSession(true);
        ServletContext application = request.getServletContext();

        ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(req.getServletContext());
        LogService logService= (LogService) ac.getBean("logService");

        Log log=new Log();
        log.setSession(session.getId());
        log.setIp(req.getRemoteAddr());
        log.setIndex(req.getRequestURI());
        log.setCreateTime(new Timestamp(session.getCreationTime()));
        log.setLastAccessedTime(new Timestamp(session.getLastAccessedTime()));
        log.setStatus("在线");
        logService.saveIpinfo(log);
        chain.doFilter(request, response);
	}

	public void destroy() {
	}

}
